package org.clipjoint.controller;

import lombok.SneakyThrows;
import org.clipjoint.entity.Game;
import org.clipjoint.entity.Purchased;
import org.clipjoint.service.GameService;
import org.clipjoint.service.UserService;
import org.clipjoint.service.impl.GameServiceImpl;
import org.clipjoint.service.impl.UserServiceImpl;
import org.clipjoint.vo.JsonResult;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


@WebServlet("/game")
public class GameController extends HttpServlet {

    private static final long serialVersionUID = 3L;

    private GameService gameService =  new GameServiceImpl();

    private UserService userService = new UserServiceImpl();

    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String method = request.getParameter("method");

        switch (method) {
            case "RPGGame":
                RPGGame(request, response);
                break;
            case "showNew":
                showNew(request, response);
                break;
            case "hot":
                hot(request, response);
                break;
            default:
                break;
        }
    }



    private void hot(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 调用service修改用户信息
        List<Purchased> hotGame = null;

        try {
            hotGame = gameService.hot();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        request.getSession().setAttribute("hotGame",hotGame);
        response.getWriter().print(JsonResult.builder().data(hotGame).code(0).build().toJsonString());
    }

    private void showNew(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 调用service修改用户信息
        List<Game> newGame = null;
        try {
            newGame = gameService.showNew();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        request.getSession().setAttribute("newGame",newGame);
        response.getWriter().print(JsonResult.builder().data(newGame).code(0).build().toJsonString());
    }

    private void RPGGame(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        // 调用service修改用户信息
        List<Game> gameList = null;
        try {
            gameList = gameService.RPGGame();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getSession().setAttribute("RPGGameList",gameList);
        response.getWriter().print(JsonResult.builder().data(gameList).code(0).build().toJsonString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
